package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.constant.UserConstant;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WeMediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Auther：DONG on 2021/11/2
 **/
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WeMediaFeign weMediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {

        //    调用mybatisplus的api进行列表查询
        //1.参数校验
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.分页参数的校验
        dto.checkParam();

        Page<ApUserRealname> page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //使用工具类进行判断是否为空

        if (null != dto.getStatus()) {
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
//        int i =2/0;  错误测试  异常
        IPage<ApUserRealname> apUserRealnamePage = page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) apUserRealnamePage.getTotal());
        pageResponseResult.setData(apUserRealnamePage.getRecords());

        return pageResponseResult;

    }

    @Override
    @GlobalTransactional
    public ResponseResult authRealName(AuthDto dto, short status) {
        //参数校验
            if (null==dto){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
            }
        //正常更新
        ApUserRealname apUserRealname = new ApUserRealname();
            apUserRealname.setId(dto.getId());
            apUserRealname.setStatus(status);
            if (StringUtils.isNotBlank(dto.getMsg())){
                apUserRealname.setReason(dto.getMsg());
            }
        boolean updateResult = updateById(apUserRealname);
                if (!updateResult){
                    return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FILE,"审核失败");
                }

            //做判断
        if (status == UserConstant.AUTH_PASS) {
            //生成自媒体账号
            ApUserRealname userRealname = this.getById(dto.getId());
            Integer userId = userRealname.getUserId();
            ApUser apUser = apUserMapper.selectById(userId);
            //通过feign查询当前app用户是否已经有自媒体账号，如果没有就进行创建
            WmUser wmUser = weMediaFeign.queryWmUserByName(apUser.getName());
            if (wmUser==null){
                wmUser=new WmUser();
                wmUser.setApUserId(apUser.getId());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setPhone(apUser.getPhone());
                wmUser.setStatus(9);
                wmUser.setCreatedTime(new Date());
                weMediaFeign.saveWmUser(wmUser);
            }
            //更新一下apUser的 falg为1
             apUser.setFlag((short)1);
                  apUserMapper.updateById(apUser);
                // 作者账号
            ApAuthor apAuthor = articleFeign.findByUserId(apUser.getId());
            if (null==apAuthor){
                apAuthor= new ApAuthor();
                apAuthor.setName(apUser.getName());
                apAuthor.setType(2);
                apAuthor.setUserId(apUser.getId());
                apAuthor.setCreatedTime(new Date());
                articleFeign.save(apAuthor);
            }
            //通过feign接口来创建


        }
//            int  i=2/0;

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
